package com.houseman.demo;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class SecurityAction implements Filter{

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		request.setCharacterEncoding("UTF-8");
		HttpServletRequest req = (HttpServletRequest)request;
		
		String userId = (String) req.getSession().getAttribute("userId");
		
		String uri = req.getRequestURI();
		
		if(userId != null || uri.startsWith("/workFlow/login")) {
			chain.doFilter(request, response);
		}else {
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

}
